What is angular-sanitize?
The angular-sanitize package is an AngularJS module that provides functionality to sanitize HTML content. It helps in preventing cross-site scripting (XSS) attacks by cleaning up the HTML content before it is displayed in the application.
What are angular-sanitize's main functionalities?
Sanitize HTML Content
This feature allows you to sanitize HTML content to prevent XSS attacks. The code sample demonstrates how to use the $sce service to trust HTML content and bind it to the scope.
angular.module('myApp', ['ngSanitize'])
.controller('myController', ['$scope', '$sce', function($scope, $sce) {
$scope.htmlContent = $sce.trustAsHtml('<p style="color:blue">Hello World</p>');
}]);
Bind HTML Content to View
This feature allows you to bind sanitized HTML content to the view using the ng-bind-html directive. The code sample shows how to bind the trusted HTML content to a div element.
<div ng-app="myApp" ng-controller="myController">
<div ng-bind-html="htmlContent"></div>
</div>
Other packages similar to angular-sanitize
dompurify
DOMPurify is a DOM-only, super-fast, uber-tolerant XSS sanitizer for HTML, MathML, and SVG. It is similar to angular-sanitize in that it provides functionality to sanitize HTML content, but it is not specific to AngularJS and can be used in any JavaScript environment.
sanitize-html
sanitize-html provides a simple HTML sanitizer with a clear API. It allows you to define a whitelist of allowed tags and attributes, making it highly customizable. Unlike angular-sanitize, it is not tied to AngularJS and can be used in any JavaScript application.
xss
xss is a module that filters input from users to prevent XSS attacks. It provides a rich set of options to customize the sanitization process. Similar to angular-sanitize, it focuses on preventing XSS attacks but is not limited to AngularJS applications.
packaged angular-sanitize
This package contains the legacy AngularJS (version 1.x). AngularJS support has officially ended
as of January 2022.
See what ending support means and
read the end of life announcement.
See @angular/core
for the actively supported Angular.
Install
You can install this package either with npm
or with bower
.
npm
npm install angular-sanitize
Then add ngSanitize
as a dependency for your app:
angular.module('myApp', [require('angular-sanitize')]);
bower
bower install angular-sanitize
Add a <script>
to your index.html
:
<script src="/bower_components/angular-sanitize/angular-sanitize.js"></script>
Then add ngSanitize
as a dependency for your app:
angular.module('myApp', ['ngSanitize']);
Documentation
Documentation is available on the
AngularJS docs site.
License
The MIT License
Copyright (c) 2022 Google LLC
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.